/*操作日志*/
function goToLog(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

/*分类设置--------------------------------------------------------------------------------------------------------------*/

//初始化加载一级品类
function goods_listCategory1(classNum, parentId) {
    var user_data = {
        category_level: classNum
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            if(data.data == null) {
                $("#classSet1").append('<li class="clearfix pl-10 mb-5">无数据</li>');
                $("#classSet1").append('<li id="firstClassInp1" style="display: none" class="clearfix pl-10 mb-5"><div class="f-l pd-5"><label class="pl-10" id="addLabel1">22</label></div><div class=" f-l class-input"><input type="text" autofocus class="input-text radius f-l firstClassId1" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addClass(1,0,\'.firstClassId1\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp1\')" class="ml-10">取消</a></div></div></li>')
            } else {
                // 标记修改
                $("#classSet1").append('<li id="firstClassInp1" style="display: none" class="clearfix pl-10 mb-5"><div class="f-l pd-5"><label class="pl-10" id="addLabel1">22</label></div><div class=" f-l class-input"><input type="text" autofocus class="input-text radius f-l firstClassId1" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addClass(1,0,\'.firstClassId1\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp1\')" class="ml-10">取消</a></div></div></li>')
                for(var i = 0; i < data.data.length; i++) {
                    $("#classSet1").append('<li class="clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="goods_listCategory2(2,this.value)" name="classification01" type="radio" value="' + data.data[i].category_id + '" class="radio" /><label class="pl-10">' + data.data[i].category_code + ' <input title="' + data.data[i].category_name + '" type="text" value="' + data.data[i].category_name + '" disabled style="height: 24px;width: 150px;border: 0px;background: white;margin-top: -4px"></label></div>')
                }
            }
        }
    };
    web.doAjax(root.goodsInter.goods_listCategory, 'post', user_data, succCallBack);
}

goods_listCategory1(1);

// 商品分类名称验证
function validClass(e) {
    var value = $(e).val();
    if (value == null || value == "") {
        $.Huimodalalert("不能为空，请输入", 1000);
        return false;
    }
    var reg = /^[a-zA-Z0-9\u4e00-\u9fa5、]{0,15}$/;
    var flag = reg.test(value);
    if (flag == false) {
        // $.Huimodalalert("格式错误，请重新输入", 2000);
        $(e).val(value.match(/[a-zA-Z0-9\u4e00-\u9fa5、]{0,15}/));// 去除非法字符
        return false;
    }
    return true;

}

function goods_listCategory2(classNum, parentId,isShowInput) {
    sessionStorage.setItem("parentId2", parentId);
    $("#classDiv2").css({
        display: "none"
    });
    $("#classDiv3").css({
        display: "none"
    });
    $("#classDiv4").css({
        display: "none"
    });
    $("#classDiv2").css({
        display: "block"
    });
    $(".removeEl2").remove();
    var user_data = {
        // category_level: classNum,
        category_id: parentId
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            if(data.data == null) {
                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5">无数据</li>');
                $("#classSet2").append('<li  class="removeEl2 clearfix pl-10 mb-5" id="firstClassInp2" style="display: none"><div class="f-l pd-5"><label class="pl-10" id="addLabel2"></label></div><div class=" f-l class-input"><input type="text" autofocus class="input-text radius f-l firstClassId2" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addClass(2,' + parentId + ',\'.firstClassId2\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp2\')" class="ml-10">取消</a></div></div></li>')
                if(isShowInput == true){
                    setTimeout(function () {
                        $("#addLabel2").html(JSON.stringify(sessionStorage.getItem("goods_addClass")).code);
                    },200)

                    $("#firstClassInp2").css({
                        display:"block"
                    })
                }
            } else {
                // 标记修改
                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5" id="firstClassInp2" style="display: none"><div class="f-l pd-5"><label class="pl-10" id="addLabel2"></label></div><div class=" f-l class-input"><input type="text" autofocus class="input-text radius f-l firstClassId2" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addClass(2,' + parentId + ',\'.firstClassId2\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp2\')" class="ml-10">取消</a></div></div></li>');
                if(isShowInput == true){
                    setTimeout(function () {
                        $("#addLabel2").html(JSON.stringify(sessionStorage.getItem("goods_addClass")).code);
                    },200)

                    $("#firstClassInp2").css({
                        display:"block"
                    })
                }
                for(var i = 0; i < data.data.length; i++) {
                    $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="goods_listCategory3(3,this.value)" name="classification02" type="radio" value="' + data.data[i].category_id + '" class="radio" /><label class="pl-10">' + data.data[i].category_code + ' <input title="' + data.data[i].category_name + '" type="text" value="' + data.data[i].category_name + '" disabled style="height: 24px;width: 100px;border: 0px;background: white;margin-top: -4px"></label></div><div class="f-r"><a onclick="firstClassAdd(this ,\'#firstClassInp3\',3,2,\'' + data.data[i].category_id + '\',\'' + data.data[i].category_code + '\')" href="##" class="Hui-iconfont icon-edit mr-10">&#xe600;</a><a onclick="eidtInpShow(2,\'#firstClassInp2\',\'#addLabel2\',' + data.data[i].category_code + ',\'.firstClassId2\',\'' + data.data[i].category_name + '\',' + data.data[i].category_id + ',' + data.data[i].parent_cate_id + ')" href="##" class="Hui-iconfont icon-edit mr-10">&#xe6df;</a><a href="##" onclick="deleteGoodsClass(' + data.data[i].category_id + ',2)" class="Hui-iconfont icon-edit mr-10">&#xe6e2;</a></div></li>')
                }
            }
        }
    };
    web.doAjax(root.goodsInter.goods_subCategory, 'post', user_data, succCallBack);

}

function goods_listCategory3(classNum, parentId,isShowInput) {
    sessionStorage.setItem("parentId3", parentId);
    $("#classDiv3").css({
        display: "block"
    });
    $(".removeEl3").remove();
    var user_data = {
        // category_level: classNum,
        category_id: parentId
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            if(data.data == null) {
                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5">无数据</li>');
                $("#classSet3").append('<li  class="removeEl3 clearfix pl-10 mb-5" id="firstClassInp3" style="display: none"><div class="f-l pd-5"><label class="pl-10" id="addLabel3"></label></div><div class=" f-l class-input"><input type="text"  autofocus class="input-text radius f-l firstClassId3" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addClass(3,' + parentId + ',\'.firstClassId3\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp3\')" class="ml-10">取消</a></div></div></li>');
                if(isShowInput == true){
                    setTimeout(function () {
                        $("#addLabel3").html(JSON.stringify(sessionStorage.getItem("goods_addClass")).code);
                    },200)
                    $("#firstClassInp3").css({
                        display:"block"
                    })
                }
            } else {
                // 获得光标
                $("#classSet3").append('<li  class="removeEl3 clearfix pl-10 mb-5" id="firstClassInp3" style="display: none"><div class="f-l pd-5"><label class="pl-10" id="addLabel3"></label></div><div class=" f-l class-input"><input type="text" autofocus class="input-text radius f-l firstClassId3" name="" placeholder=""><div class="f-l"><span class="msgError" style="color: red;"></span></div><div class="f-r pt-5"><a href="##" onclick="addClass(3,' + parentId + ',\'.firstClassId3\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp3\')" class="ml-10">取消</a></div></div></li>');
                if(isShowInput == true){
                    setTimeout(function () {
                        $("#addLabel3").html(JSON.stringify(sessionStorage.getItem("goods_addClass")).code);
                    },200);

                    $("#firstClassInp3").css({
                        display:"block"
                    })
                }
                for(var i = 0; i < data.data.length; i++) {
                    $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="goods_listCategory4(4,this.value)" name="classification03" type="radio" value="' + data.data[i].category_id + '" class="radio" /><label class="pl-10">' + data.data[i].category_code + ' <input title="' + data.data[i].category_name + '" type="text" value="' + data.data[i].category_name + '" disabled style="height: 24px;width: 100px;border: 0px;background: white;margin-top: -4px"></label></div><div class="f-r"><a href="##" onclick="eidtInpShow(2,\'#firstClassInp3\',\'#addLabel3\',' + data.data[i].category_code + ',\'.firstClassId3\',\'' + data.data[i].category_name + '\',' + data.data[i].category_id + ',' + data.data[i].parent_cate_id + ')" class="Hui-iconfont icon-edit mr-10">&#xe6df;</a><a href="##" onclick="deleteGoodsClass(' + data.data[i].category_id + ',3)" class="Hui-iconfont icon-edit mr-10">&#xe6e2;</a></div></li>')
                }

            }
        }
    };
    return web.doAjax(root.goodsInter.goods_subCategory, 'post', user_data, succCallBack);
}

function goods_listCategory4(classNum, parentId) {
    sessionStorage.setItem("parentId4", parentId);
    $("#classDiv4").css({
        display: "block"
    });
    $(".removeEl4").remove();
    var user_data = {
        // category_level: classNum,
        category_id: parentId
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            if(data.data == null) {
                $("#classSet4").append('<li class="removeEl4 clearfix pl-10 mb-5">无数据</li>');
                $("#classSet4").append('<li  class="removeEl4 clearfix pl-10 mb-5" id="firstClassInp4" style="display: none"><div class="f-l pd-5"><input name="classification04" type="radio" value="" class="radio" /><label class="pl-10" id="addLabel4"></label></div><div class=" f-l class-input"><input type="text"  autofocus class="input-text radius f-l firstClassId4" maxlength="15" name="" placeholder=""><div class="f-l pt-5"><a href="##" onclick="addClass(4,' + parentId + ',\'.firstClassId4\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp4\')" class="ml-10">取消</a></div></div></li>')
            } else {
                $("#classSet4").append('<li  class="removeEl4 clearfix pl-10 mb-5" id="firstClassInp4" style="display: none"><div class="f-l pd-5"><input name="classification04" type="radio" value="" class="radio" /><label class="pl-10" id="addLabel4"></label></div><div class=" f-l class-input"><input type="text" autofocus  class="input-text radius f-l firstClassId4" maxlength="15" name="" placeholder=""><div class="f-l pt-5"><a href="##" onclick="addClass(4,' + parentId + ',\'.firstClassId4\')" class="ml-10">确认</a><a href="##" onclick="celClassFirstAdd(\'#firstClassInp4\')" class="ml-10">取消</a></div></div></li>')
                for(var i = 0; i < data.data.length; i++) {
                    $("#classSet4").append('<li class="removeEl4 clearfix pl-10 mb-5"><div class="f-l pd-5"><input name="classification04" type="radio" value="' + data.data[i].category_id + '" class="radio" /><label class="pl-10">' + data.data[i].category_code + ' <input title="' + data.data[i].category_name + '" type="text" value="' + data.data[i].category_name + '" disabled style="height: 24px;width: 100px;border: 0px;background: white;margin-top: -4px"></label></div><div class="f-r"><a href="##" class="Hui-iconfont icon-edit mr-10">&#xe600;</a><a href="##" onclick="eidtInpShow(\'#firstClassInp4\',' + data.data[i].category_id + ')" class="Hui-iconfont icon-edit mr-10">&#xe6df;</a><a href="##" onclick="deleteGoodsClass(' + data.data[i].category_id + ',4)" class="Hui-iconfont icon-edit mr-10">&#xe6e2;</a></div></li>')
                }

            }
        }
    };
    return web.doAjax(root.goodsInter.goods_subCategory, 'post', user_data, succCallBack);
}

/*添加一级分类*/
function firstClassAdd(thisid,classId, labelId, categoryLevel, categoryId, categoryCode) {
    $(".firstClassId1").css({
        border: "1px #ddd solid"
    })
    $(".msgError").html("")
    if(categoryLevel == 1){// goods_listCategory2(级别,单选框值,是否显示输入框);
        thisid.parentElement.parentElement.firstChild.firstChild.checked = "checked";//点击加号出发单选框
        goods_listCategory2(2,thisid.parentElement.parentElement.firstChild.firstChild.value,true);
    }else if(categoryLevel == 2){
        thisid.parentElement.parentElement.firstChild.firstChild.checked = "checked";//点击加号出发单选框
        goods_listCategory3(3,thisid.parentElement.parentElement.firstChild.firstChild.value,true);
    }
    $(".firstClassId1").val("");
    var container = $('.bk-gray');
    $(container).scrollTop(0);
    sessionStorage.setItem("isGoodsClassEdit", false);
    $(classId).css({
        display: "block"
    });

    // 设置获得光标
    $(classId).children().children("input").focus();
    var user_data = {
        "categoryLevel": categoryLevel,
        "categoryId": categoryId,
        "categoryCode": categoryCode
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            // alert(888)
            setTimeout(function () {
                $("#addLabel" + labelId + "").html(data.data.code);
            },200)
            sessionStorage.setItem('goods_addClass', JSON.stringify(data.data));
        }
    };
    return web.doAjax(root.goodsInter.goods_getCateCode, 'post', user_data, succCallBack);
}

function eidtInpShow(can, className, labelName, categorycode, inpName, categoryName, categoryId, parent_cate_id) {
    $(inpName).css({
        border: "1px #ddd solid"
    })
    $(".msgError").html("")
    var container = $('.bk-gray');
    $(container).scrollTop(0);
    sessionStorage.setItem("isGoodsClassEdit", true);
    sessionStorage.setItem("categoryId", categoryId);
    sessionStorage.setItem("parent_cate_id", parent_cate_id);
    $(className).css({
        display: "block"
    });
    $(className).children().children("input").focus();
    $(labelName).html(categorycode);
    $(inpName).val(categoryName);
}

function addClass(categoryLevel, parentCateId, firstClassId) {
    var isGoodsClassEdit = sessionStorage.getItem("isGoodsClassEdit");
    // var inputval = $(firstClassId).val();
    // if(inputval == null || inputval == '') {
    // 	$.Huimodalalert("不能为空，请输入", 2000)
    // 	return;
    // }
    //获取用户信息
    var localStorageParameters = web.readCookie("loginData");
    var JsonObject =JSON.parse(localStorageParameters);
    if(isGoodsClassEdit == "true") {
        if($(firstClassId).val() == "") {
            $(firstClassId).css({
                border: "1px red solid"
            })
            $(".msgError").html("<img src='../../lib/Validform/images/error.png'>分类名称不能为空")
        }else{
            if($(firstClassId).val().length > 15){
                $(firstClassId).css({
                    border: "1px red solid"
                })
                $(".msgError").html("<img src='../../lib/Validform/images/error.png'>格式错误请重新输入")
            }else {
                var user_data = {
                    parentCateId: sessionStorage.getItem("parent_cate_id"),
                    categoryId: sessionStorage.getItem("categoryId"),
                    categoryName: $(firstClassId).val(),
                    lastModifiedBy:JsonObject.user.user_ID,
                }
                var succCallBack = function(data, status, response) {
                    if(data.errorCode == "0") {
                        layer.msg("修改成功", {
                            icon: 6,
                            time: 1000
                        }, function() {
                            // window.location.reload();
                            if(categoryLevel == 1) {
                                window.location.reload();
                            } else if(categoryLevel == 2) {
                                var parentId1 = window.sessionStorage.getItem("parentId2")
                                goods_listCategory2(categoryLevel, parentId1)
                            } else if(categoryLevel == 3) {
                                var parentId1 = window.sessionStorage.getItem("parentId3")
                                goods_listCategory3(categoryLevel, parentId1)
                            } else if(categoryLevel == 4) {
                                var parentId1 = window.sessionStorage.getItem("parentId4")
                                goods_listCategory4(categoryLevel, parentId1)
                            }
                        })
                    } else {
                        layer.msg(data.errorMessage, {
                            icon: 5,
                            time: 1000
                        })
                    }
                };
                return web.doAjax(root.goodsInter.goods_updateCategory, 'post', user_data, succCallBack);
            }
        }
    } else {
        if($(firstClassId).val() == "") {
            $(firstClassId).css({
                border: "1px red solid"
            })
            $(".msgError").html("<img src='../../lib/Validform/images/error.png'>分类名称不能为空")
        }else{
            if($(firstClassId).val().length > 15){
                $(firstClassId).css({
                    border: "1px red solid"
                })
                $(".msgError").html("<img src='../../lib/Validform/images/error.png'>格式错误请重新输入")
            }else {
                var classCodes = JSON.parse(sessionStorage.getItem("goods_addClass"));
                var user_data = {
                    "categoryLevel": categoryLevel,
                    "categoryCode": classCodes.categoryCode,
                    "code": classCodes.code,
                    "categoryName": $(firstClassId).val(),
                    "parentCateId": parentCateId,
                    "creationBy":JsonObject.user.user_ID
                }
                var succCallBack = function(data, status, response) {
                    if(data.errorCode == "0") {
                        layer.msg("添加成功", {
                            icon: 6,
                            time: 1000
                        })
                        setTimeout(function() {
                            // alert("添加成功")
                            $("#firstClassInp1").hide()
                            // var func="goods_listCategory"+categoryLevel()
                            // window[func]();
                            if(categoryLevel == 1) {
                                window.location.reload();
                            } else if(categoryLevel == 2) {
                                var parentId1 = window.sessionStorage.getItem("parentId2")
                                goods_listCategory2(categoryLevel, parentId1)
                            } else if(categoryLevel == 3) {
                                var parentId1 = window.sessionStorage.getItem("parentId3")
                                goods_listCategory3(categoryLevel, parentId1)
                            } else if(categoryLevel == 4) {
                                var parentId1 = window.sessionStorage.getItem("parentId4")
                                goods_listCategory4(categoryLevel, parentId1)
                            }
                        }, 200)
                    } else {
                        var calssfir = ".firstClassId" + categoryLevel
                        $(calssfir).val("")
                        layer.msg(data.errorMessage, {
                            icon: 5,
                            time: 1000
                        })
                    }
                };
                return web.doAjax(root.goodsInter.goods_addCategory, 'post', user_data, succCallBack);
            }
        }
    }
}

/*删除分类*/
function deleteGoodsClass(classId, categoryLevel) {
    var localStorageParameters = web.readCookie("loginData");
    var JsonObject =JSON.parse(localStorageParameters);
    layer.confirm('确认要删除吗？', function(index) {
        var class_data = {
            categoryId: classId,
            lastModifiedBy:JsonObject.user.user_ID
        }
        var succCallBack = function(data, status, response) {
            if(data.errorCode == "0") {
                layer.msg("删除成功", {
                    icon: 6,
                    time: 1000
                }, function() {
                    if(categoryLevel == 1) {
                        window.location.reload();
                    } else if(categoryLevel == 2) {
                        var parentId1 = window.sessionStorage.getItem("parentId2")
                        goods_listCategory2(categoryLevel, parentId1)
                    } else if(categoryLevel == 3) {
                        var parentId1 = window.sessionStorage.getItem("parentId3")
                        goods_listCategory3(categoryLevel, parentId1)
                    } else if(categoryLevel == 4) {
                        var parentId1 = window.sessionStorage.getItem("parentId4")
                        goods_listCategory4(categoryLevel, parentId1)
                    }
                })
            } else {
                layer.msg(data.errorMessage, {
                    icon: 5,
                    time: 1000
                }, function() {})
            }
        };
        return web.doAjax(root.goodsInter.goods_delCategory, 'post', class_data, succCallBack);
    });
}